import requests
import json
from pathlib import Path

def fetch_and_save_holiday_data(year: str = "2025"):
    """
    获取指定年份的节假日数据并保存到文件
    
    Args:
        year: 年份，默认2025
    """
    # API URL
    url = f"https://timor.tech/api/holiday/year/{year}"
        # 添加请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
    }
    try:
        print(f"开始获取{year}年的节假日数据，请求的 URL: {url}")
        # 发送GET请求
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 检查请求是否成功
        
        # 获取JSON数据
        holiday_data = response.json()
        
        # 保存到文件
        output_file = Path("currentYearHoliday.json")
        with output_file.open('w', encoding='utf-8') as f:
            json.dump(holiday_data, f, ensure_ascii=False, indent=2)
            
        print(f"节假日数据已保存到: {output_file.absolute()}")
        return True
        
    except requests.RequestException as e:
        print(f"请求失败: {e}")
        return False
    except json.JSONDecodeError as e:
        print(f"JSON解析失败: {e}")
        return False
    except IOError as e:
        print(f"文件写入失败: {e}")
        return False

if __name__ == "__main__":
    fetch_and_save_holiday_data()